#ifndef GUI_H_
#define GUI_H_

/*
	Problems:
	* TextBox padding; the text isn't vertically centered
	* TextBox Anchoring works only for one line of text; write func needs to be extended
*/

#include "CFont.h"
#include <vector>
using namespace std;

//Anchors
enum Anchor {LEFT_ANCHOR, CENTER_ANCHOR, RIGHT_ANCHOR,}; // CENTER is default

class GUICanvas;

class GUIManager
{
private:
	unsigned globalID;				// keeps track of what the next id is
	vector<GUICanvas> canvas;
public:
	GUIManager();
	~GUIManager();
	void AddCanvas(GUICanvas & canvas);
	void RemoveCanvas(unsigned index);
	void DrawCanvas();
	void Mouse(unsigned x, unsigned y);
	unsigned GetID() {globalID++; return globalID-1;}
};

class GUICanvas
{
private:
	unsigned id;					// used to identify the children
	unsigned width;                 
	unsigned height;              
	unsigned x1,y1,x2,y2;           // top left, bottom right

protected:
	unsigned type;					// the type of element
	unsigned borderThickness;		// line thickness of the border
	unsigned padding;				// in pixels on all sides
	unsigned offset;				// where the next element should be placed (vertically)
	float bgColor[3];				
	float borderColor[3];		
	float alpha;					// transparency for it and it's children
	bool hide;
	vector<GUICanvas*> children;		// contains all elements linked to it

public:
	GUICanvas();
	GUICanvas(unsigned x, unsigned y, unsigned width, unsigned height, unsigned id=0);
	~GUICanvas();

	enum {CANVAS,TEXTBOX,};
	virtual void Draw();
	void Show() {hide=0;}
	void Hide() {hide=1;}

	void AddChild(GUICanvas * child);	
	void EraseChild(unsigned index);	// not implemented
	void IDChild(unsigned id);			// not implemented
	virtual void Mouse(unsigned x, unsigned y);

	bool GetState() const {return hide;}
	float GetAlpha() const {return alpha;}
	unsigned GetTopX() const {return x1;}
	unsigned GetTopY() const {return y1;}
	unsigned GetBottomX() const {return x2;}
	unsigned GetBottomY() const {return y2;}
	unsigned GetWidth() const {return width;}
	unsigned GetHeight() const {return height;}
	unsigned GetID() const {return id;}

	void SetXY1(unsigned x, unsigned y) {x1=x;y1=y;}
	void SetXY2(unsigned x, unsigned y) {x2=x;y2=y;}
	void SetWidth(unsigned w) {width=w;}
	void SetHeight(unsigned h) {height=h;}
	void SetPadding(unsigned p) {padding=p;}
	void SetID(unsigned id) {this->id=id;}
	void SetAlpha(float a) {alpha=a;}
	void SetBorderThickness(unsigned a) {borderThickness=a;}
	void SetBGColor(float r, float g, float b) {bgColor[0]=r; bgColor[1]=g; bgColor[2]=b;}
	void SetBorderColor(float r, float g, float b) {borderColor[0]=r; borderColor[1]=g; borderColor[2]=b;}
};


class GUITextBox : public GUICanvas
{
private:
	CFont * font;
	unsigned x,y;
	unsigned charsPerLine; 
	unsigned numLines;
	unsigned maxLines;
	unsigned currLine; 
	unsigned charHeight;
	unsigned charLength;
	unsigned numChar;		// the highest number of chars in any line of the text; used for anchoring;
	Anchor anchor;
	bool textWrap;
	bool writable;
	float textColor[3];
	char ** text; 
	void (*MouseFunc)(void);

public:
	GUITextBox();
	GUITextBox(const GUITextBox & textField);
	void CreateField(CFont & font, unsigned x, unsigned y, unsigned numChars, unsigned numLines, unsigned maxLines);
	void Write(const char * text_, ...);
	void SetAnchor(Anchor anchor);
	void SetTextColor(float r, float g, float b) {textColor[0]=r; textColor[1]=g;textColor[2]=b;}
	void SetMouseFunc(void (*MouseFunc)(void));

	virtual void Draw(void);
	virtual void Mouse(unsigned x, unsigned y);

	GUITextBox & operator=(const GUITextBox & textField); 
	~GUITextBox();
};

#endif
